package main

import "flag"
import "time"
import "fmt"

//来自tgpl ch7.4
var period = flag.Duration("period",1*time.Second, "sleep period")

func t() {
	flag.Parse()
	fmt.Printf("sleeping for %v...",*period)
	time.Sleep(*period)
	fmt.Println("done")
}


///////////// define flag notations for our own data type
type Celsius float64
type Fahrenheit float64

func (c Celsius) String() string {
	return fmt.Sprintf("%g ^C",c)
}
type celsiusFlag struct { Celsius }

//使celsiusFlag实现flag.Value接口，有两个方法，String和Set
func (f *celsiusFlag) Set(s string) error {
	var unit string
	var value float64
	fmt.Scanf(s,"%f%s",&value,&unit)//no error check needed
	switch unit {
	case "C","^C":
		f.Celsius = Celsius(value)
		return nil
	case "F","^F":
		//f.Celsius = FToC(Fahrenheit(value))
		return nil
	}
	return fmt.Errorf("invalid temp %q",s)
}

func CelsiusFlag(name string, value Celsius, usage string) *Celsius {
	f := celsiusFlag{value}
	flag.CommandLine.Var(&f,name,usage)
	return &f.Celsius
}

var temp = CelsiusFlag("temp",20.0,"the tempreature")
func t_define_new_notations_for_custom_type() {
	
	flag.Parse()
	fmt.Println(*temp)
}

func main() {
	t_define_new_notations_for_custom_type()
	//t()
}